﻿/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/24/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoContentPage
    {
        public daoContentPage()
        {

        }        
        private ContentPage Load(IDataReader de)
        {
            ContentPage item = new ContentPage();
            item.Content = clsConvert.DbToString("Content", de);
            item.MetaDescription = clsConvert.DbToString("MetaDescription", de);
            item.PageId = clsConvert.DbToInt("PageId", de);            
            item.Title = clsConvert.DbToString("Title", de);
            item.PageURL = clsConvert.DbToString("PageURL", de);
            item.MetaKeywords = clsConvert.DbToString("MetaKeywords", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            item.Language = clsConvert.DbToInt("Language", de);
            return item;            
        }
        public int Insert(ContentPage objContentPage)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[9];
            try
            {
                para[0] = new SqlParameter(busContentPage.PAGEURL, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objContentPage.PageURL);
                para[1] = new SqlParameter(busContentPage.NAME, SqlDbType.NVarChar);
                para[1].Value = objContentPage.Name;
                para[2] = new SqlParameter(busContentPage.TITLE, SqlDbType.NVarChar);
                para[2].Value = objContentPage.Title;
                para[3] = new SqlParameter(busContentPage.CONTENT, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objContentPage.Content);
                para[4] = new SqlParameter(busContentPage.METADESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objContentPage.MetaDescription);
                para[5] = new SqlParameter(busContentPage.METAKEYWORDS, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objContentPage.MetaKeywords);
                para[6] = new SqlParameter(busContentPage.CREATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objContentPage.CreatedBy);
                para[7] = new SqlParameter(busContentPage.CREATEDDATE, SqlDbType.DateTime);
                para[7].Value = objContentPage.CreatedDate;
                para[8] = new SqlParameter(busContentPage.LANGUAGE, SqlDbType.TinyInt);
                para[8].Value = objContentPage.Language;
                result = clsSQL.SPExecute("ContentPage_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentPage", "Insert(ContentPage objContentPage)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }            
            return result;
        }      
        
        public bool Delete(int pageID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busContentPage.PAGEID, SqlDbType.Int);
                para[0].Value = pageID;
                result = clsSQL.SPExecute("ContentPage_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentPage", "Delete(int pageID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public ContentPage GetByID(int pageID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            ContentPage item = new ContentPage();
            try
            {
                para[0] = new SqlParameter(busContentPage.PAGEID, SqlDbType.Int);
                para[0].Value = pageID;
                using (de = clsSQL.SPDataReader("ContentPage_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoContentPage", " GetByID(int pageID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public ContentPage GetByURL(string url)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            ContentPage item = new ContentPage();
            try
            {
                para[0] = new SqlParameter(busContentPage.PAGEURL, SqlDbType.VarChar);
                para[0].Value = url;
                para[1] = new SqlParameter(busContentPage.LANGUAGE, SqlDbType.TinyInt);
                para[1].Value = clsSession.GetLanguage();
                using (de = clsSQL.SPDataReader("ContentPage_GetByURL", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoContentPage", "GetByURL(string url)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public int Update(ContentPage objContentPage)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[10];
            try
            {
                para[0] = new SqlParameter(busContentPage.PAGEURL, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objContentPage.PageURL);
                para[1] = new SqlParameter(busContentPage.NAME, SqlDbType.NVarChar);
                para[1].Value = objContentPage.Name;
                para[2] = new SqlParameter(busContentPage.TITLE, SqlDbType.NVarChar);
                para[2].Value = objContentPage.Title;
                para[3] = new SqlParameter(busContentPage.CONTENT, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objContentPage.Content);
                para[4] = new SqlParameter(busContentPage.METADESCRIPTION, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objContentPage.MetaDescription);
                para[5] = new SqlParameter(busContentPage.METAKEYWORDS, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objContentPage.MetaKeywords);
                para[6] = new SqlParameter(busContentPage.UPDATEDBY, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objContentPage.UpdatedBy);
                para[7] = new SqlParameter(busContentPage.UPDATEDDATE, SqlDbType.DateTime);
                para[7].Value = objContentPage.UpdatedDate;              
                para[8] = new SqlParameter(busContentPage.PAGEID, SqlDbType.Int);
                para[8].Value = objContentPage.PageId;
                para[9] = new SqlParameter(busContentPage.LANGUAGE, SqlDbType.TinyInt);
                para[9].Value = objContentPage.Language;
                result = clsSQL.SPExecute("ContentPage_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoContentPage", "Update(ContentPage objContentPage)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public ContentPageCollection List(ContentPage objContentPage)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            ContentPageCollection items = new ContentPageCollection();
            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objContentPage.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objContentPage.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objContentPage.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = objContentPage.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objContentPage.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("ContentPage_List", para);
                while (de.Read())
                {
                    ContentPage item = Load(de);
                    items.Add(item);
                }
                de.Close();
                objContentPage.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoContentPage", "List(TipNews objTipNews)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }

        public ContentPageCollection GetAll(ContentPage objContentPage)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            ContentPageCollection items = new ContentPageCollection();
            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objContentPage.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objContentPage.OrderDirection;
                de = clsSQL.SPDataReader("ContentPage_GetAll", para);
                while (de.Read())
                {
                    ContentPage item = Load(de);
                    items.Add(item);
                }
                de.Close();              
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoContentPage", "GetAll(ContentPage objContentPage)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
    }
}


